package com.zhangtao.demo.controller;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhangtao.demo.common.config.GloableConfig;
import com.zhangtao.demo.common.enums.ResultStatus;
import com.zhangtao.demo.common.util.BaiduSpeechApi;
import com.zhangtao.demo.common.util.Sha256Util;
import com.zhangtao.demo.common.vo.JsonResponseEntity;

@RequestMapping("/voice")
@RestController
public class VoiceController {

	@Autowired
	private GloableConfig gconfig;
	@Autowired
	private BaiduSpeechApi bsApi;

	@GetMapping(value = "/generateVoice")
	public JsonResponseEntity<String> generateVoiceByContent(
			@RequestParam(value = "langType", defaultValue = "zh") String langType,
			@RequestParam(value = "content") String content) {
		String voiceFile = String.format("%s%s_output.mp3", langType, Sha256Util.getSHA256(content));
		// bsApi.textTransfertoZHVoiceFile(content, resourceClassPath + voiceFile);
		File oldFile = new File(gconfig.getLocalResourcePath() + File.separator + voiceFile);
		if (!oldFile.exists()) {
			bsApi.textTransfertoVoiceWithLang(content, gconfig.getLocalResourcePath() + File.separator + voiceFile,
					langType);
		}
		return new JsonResponseEntity<String>(ResultStatus.SUCCESS,
				gconfig.getRemoteFileUrl() + File.separator + voiceFile);
	}

}
